var tree_store;
var menu_tree;
var form_panel;
Ext
		.onReady(function() {

			var tree_url = rootPath + "/menu/getMenuTree.do";
			tree_store = Ext.create('Ext.data.TreeStore', {
				storeId : 'tree_store_',
				fields : [ 'id', 'text', 'pid', 'level', 'url', 'seq' ],
				proxy : {
					type : 'ajax',
					url : tree_url,
					reader : {
						type : 'json',
						root : 'tree_items'
					}
				}
			});
			menu_tree = Ext.create('Ext.tree.Panel', {
				id : 'menu_tree_',
				title : '菜单',
				selModel : true,
				rootVisible : true,
				store : tree_store,
				autoHeight : true,
				minHeight : 400,
				margin : '2 0 0 0',
				height : document.body.scrollHeight - 60,
				root : {
					expanded : true,
					id : "1",
					text : '菜单',
					level : 1
				},
				draggable : true
			});
			menu_tree.on("itemclick", treeClick);

			var menu_panel = Ext.create('Ext.toolbar.Toolbar', {
				margin : '0 0 2px 0',
				items : [ {
					text : '增加',
					handler : addNode
				}, {
					text : '删除',
					handler : delNode
				} ]
			});

			form_panel = Ext.create("Ext.form.Panel", {
				title : '配置',
				autoWidth : true,
				autoHeight : true,
				defaults : {
					xtype : 'textfield',
					labelAlign : "right",
					labelWidth : 100
				},
				items : [ {
					xtype : 'hiddenfield',
					name : 'pid',
					id : 'pid'
				},{
					fieldLabel : 'id',
					xtype : 'textfield',
					name : 'id',
					id : 'id',
					readOnly:true
				}, {
					fieldLabel : 'text',
					name : 'text',
					id : 'text'
				}, {
					fieldLabel : 'url',
					name : 'url',
					id : 'url'
				}, {
					xtype : 'textfield',
					fieldLabel : 'level',
					name : 'level',
					id : 'level',
					readOnly:true
				}, {
					fieldLabel : 'leaf',
					name : 'leaf',
					id : 'leaf'
				}, {
					xtype : 'numberfield',
					fieldLabel : 'seq',
					name : 'seq',
					id : 'seq'
				} ],
				buttons : [ {
					text : 'save',
					handler : save
				} ]
			});

			var iframe_html = '<iframe scrolling="auto" id="iframe_center" frameborder="0" width="100%" height="100%" src="" ></iframe>';
			Ext.create('Ext.container.Viewport', {
				layout : 'border',
				renderTo : Ext.getBody(),
				items : [ {
					xtype : 'panel',
					region : 'west',
					title : '配置',
					collapsible : true,
					width : "20%",
					split : true,
					items : [ menu_tree, menu_panel ]
				}, {
					region : 'center',
					items : [ form_panel ]
				} ]
			});
		});

function addNode() {
	var sel = menu_tree.getSelectionModel().getSelection();
	if (sel.length == 0) {
		Ext.Msg.alert('提示', '先选择节点');
		return false;
	}
	var m = sel[0];
	if (m.get("leaf")) {
		Ext.Msg.alert('提示', '该节点为叶子节点，不能添加子节点');
		return false;
	}
	m.expand(false, function() {
		m.appendChild({
			pid:m.data.id,
			text : 'new',
			leaf : true,
			level : m.data.level + 1
		});
		var newNode = m.lastChild;
		newNode.isNew = true;
		console.log(newNode);
		menu_tree.getSelectionModel().select(newNode);
		menu_tree.fireEvent("itemclick", menu_tree, newNode);
	});

}
function delNode() {
	var sel = menu_tree.getSelectionModel().getSelection();
	if (sel.length == 0) {
		Ext.Msg.alert('提示', '先选择节点');
		return false;
	}
	var m = sel[0];

	if (m.isNew) {
		var p = m.parentNode;
		m.remove(true);// 参数为true才能destory
		return false;
	}
	//TODO
	Ext.Ajax.request({
		url:'delete.do',
		params:{
			id:m.data.id
		},
		success:function(a,b){
			console.log(a,b);
			form_panel.getForm().reset();
			tree_store.load();
		}
	});
	

}
function treeClick(view, record, item, index, e) {
	
	form_panel.getForm().reset();	
	form_panel.loadRecord(record);
	console.log(record);
	if (!record.get("leaf")){
		Ext.getCmp("leaf").setReadOnly(true);
//		return;
	}else{
		Ext.getCmp("leaf").setReadOnly(false);
	}

}
function save() {
	var form = form_panel.getForm();
	var l = Ext.getCmp("leaf").getValue();
	if (l == "true") {
		Ext.getCmp("leaf").setValue(1);
	} else if (l == "false") {
		Ext.getCmp("leaf").setValue(0);
	}
	if (form.isValid()) {
		
		var rec = form.getRecord();
		form.submit({
			url : 'save.do',
			params:{
				pid:Ext.getCmp("pid").getValue()
			},
			success : function(form, action) {
				Ext.Msg.alert('Success', "操作成功");
				form.reset();
				tree_store.load();
			},
			failure : function(form, action) {
				Ext.Msg.alert('Failed', action.result.msg);
			}
		});
	}

}
